setwd(here())

source(here("renv", "activate.R"))

library(conflicted)

conflict_prefer("filter", "dplyr", quiet = TRUE)
conflict_prefer("count", "dplyr", quiet = TRUE)
conflict_prefer("summarize", "dplyr", quiet = TRUE)
conflict_prefer("group_rows", "dplyr", quiet = TRUE)
conflict_prefer("timestamp", "utils", quiet = TRUE)

base_dir <- here()
tab_dir <- here("results", "tables")
fig_dir <- here("results", "figures")

library(tidyverse)
library(fixest)
library(arrow)
library(kableExtra)
library(ggtext)
library(haven)

source(here("code", "stat_binscatter.R"))

setFixest_dict(
  c(
    "vet_combined" = "Veteran",
    "order_num_serial_norm" = "Order number (scaled)",
    "birthyr_cards" = "Birth year",
    "bpl_cards" = "Birth state",
    "exemption" = "Exemption",
    "married_cards" = "Married",
    "farmer" = "Farmer",
    "laborer" = "Laborer",
    "farm_laborer" = "Farm laborer",
    "board_identifier" = "Draft board",
    "is_naacp" = "NAACP member"
  )
)

setFixest_etable(
  digits.stats = 3,
  depvar = TRUE,
  meta.time = TRUE,
  style.tex = style.tex(
    main = "aer",
    fixef.suffix = "",
    fixef.where = "var"
  )
)

fig_base_size <- 13

my_theme <- function() {
  theme_light(base_size = fig_base_size) %+replace%
    theme(
      panel.grid.minor = element_blank(),
      panel.grid.major.x = element_blank(),
      panel.border = element_blank(),
      panel.spacing = unit(2, "lines"),
      axis.ticks.y = element_blank(),
      axis.ticks.x = element_line(color = "black", linewidth = .25),
      axis.ticks.length = unit(.15, "cm"),
      axis.text.x = element_text(margin = margin(t = 5)),
      strip.background = element_rect(fill = NA, color = NA),
      strip.text = element_text(color = "black"),
      strip.text.x = element_text(size = 9, margin = unit(rep(2, 4), "pt"), face = "bold"),
      legend.position = "top",
      axis.title.x = element_text(vjust = -1)
    )
}

linesep <- function(x, y = character()) {
  if (!length(x))
    return(y)
  linesep(x[-length(x)], c(rep('', x[length(x)] - 1), "\\addlinespace", y))
}
